﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player_Fox : MonoBehaviour
{
    public Rigidbody2D rb;
    public Collider2D coll;
    public Animator anim;
    public Transform stampPoint;
    public float moveSpeed = 5f;
    public float moveSpeedGround = 5f;
    public float moveSpeedAir = 2f;
    public float jumpForce = 5f;
    public LayerMask ground;

    bool isGround;
    bool isJump;
    bool jumpPressed;
    int jumpCount;


    void Awake()
    {
        
    }

    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        coll = GetComponent<Collider2D>();
        anim = GetComponent<Animator>();
    }
    
    void Update()
    {
        //if (Input.GetButtonDown("Jump") && jumpCount > 0)
        if (Input.GetButtonDown("Jump"))
        {
            jumpPressed = true;
        }
    }

    void FixedUpdate()
    {
        GroundMovement();
        Jump();
        Stamp();
        SwitchAnim();
    }

    //踩怪攻击
    public void Stamp()
    {
        Collider2D c = Physics2D.OverlapCircle(stampPoint.position, 0.1f, LayerMask.GetMask("Enemy"));
        if (c == null)
        {
            return;
        }
        //Debug.Log("踩到怪物");
        
        Opossum opossum = c.gameObject.GetComponent<Opossum>();
        if (opossum != null)
        {
            opossum.Dead();
        }

        rb.velocity = new Vector2(rb.velocity.x, 0);
        rb.AddForce(new Vector2(0, 500f));
    }
    public void GroundMovement()
    {
        float horizontalMove = Input.GetAxis("Horizontal");
        
        if (anim.GetBool("jumping"))
        {
            moveSpeed = moveSpeedAir;
        }
        else
        {
            moveSpeed = moveSpeedGround;
        }

        rb.velocity = new Vector2(horizontalMove * moveSpeed, rb.velocity.y);

        if (horizontalMove > 0)
        {
            transform.localScale = new Vector3(Mathf.Abs(transform.localScale.x), transform.localScale.y, transform.localScale.z);
        }
        else if (horizontalMove < 0)
        {
            transform.localScale = new Vector3(-Mathf.Abs(transform.localScale.x), transform.localScale.y, transform.localScale.z);
        }
        anim.SetFloat("running", Mathf.Abs(horizontalMove));
    }

    public void Jump()
    {
        if (isGround)
        {
            jumpCount = 2;
            isJump = false;
        }

        if (jumpPressed)
        {
            isJump = true;
            rb.velocity = new Vector2(rb.velocity.x, jumpForce);
            jumpCount--;
            jumpPressed = false;

            anim.SetBool("jumping", true);
        }
    }

    public void SwitchAnim()
    {
        anim.SetBool("idle", false);
        if (anim.GetBool("jumping"))
        {
            if (rb.velocity.y < 0)
            {
                anim.SetBool("jumping", false);
                anim.SetBool("falling", true);
            }
        }
        else if (coll.IsTouchingLayers(ground))
        {
            anim.SetBool("falling", false);
            anim.SetBool("idle", true);
        }
    }
}
